/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.PrivilegChecker;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.entity.MappingServiceVersion;
import han.bifang.gwt.client.common.entity.VersionQos;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.ClickListener;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.SimplePanel;
import com.google.gwt.user.client.ui.SourcesTabEvents;
import com.google.gwt.user.client.ui.TabListener;
import com.google.gwt.user.client.ui.TabPanel;
import com.google.gwt.user.client.ui.VerticalPanel;
import com.google.gwt.user.client.ui.Widget;

/**
 * 
 * @Author Kent
 */
public class RemoteVersionRootContent extends Content {
	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private ServiceMessages cons;
	private SimplePanel panel = null;
	private String id = null;
	private String version = null;
	private TabPanel tab ;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			ArrayList ds = (ArrayList) res.getResponse();

			MappingServiceVersion v = (MappingServiceVersion) ds.get(0);
			version = v.getVersion();
			ArrayList<VersionQos> qos = (ArrayList<VersionQos>) ds.get(1);

			VerticalPanel v1 = new VerticalPanel();
			v1.setWidth("100%");
			tab = new TabPanel();
			tab.add(new RemoteVersionBasicContent(cons, service, context, v,
					qos), cons.basic());
			tab
					.add(new RemoteVersionUserMappingContent(cons, service,
							context), cons.userMapping());
			tab.add(new VersionSchemaContent(cons, service, v.getId()),cons.schema());
			tab.addTabListener(new TabListener() {

				public boolean onBeforeTabSelected(SourcesTabEvents sender,
						int tabIndex) {
					if (tabIndex == 2) {
							VersionSchemaContent t = (VersionSchemaContent) tab
									.getWidget(2);
							t.init();
						
					}
					return true;
				}

				public void onTabSelected(SourcesTabEvents sender, int tabIndex) {
					// TODO Auto-generated method stub

				}

			});

			tab.selectTab(0);
			tab.setWidth("100%");

			v1.add(tab);
			v1.add(new HTML("&nbsp;"));

			Button b1 = new Button(cons.remove());
			b1.addClickListener(new ClickListener() {

				public void onClick(Widget sender) {
					HashMap p = new HashMap();
					p.put("id", id);
					p.put("version", version);
					listener.onContentChanged(
							ServiceContentConstants.REMOTE_VERSION_ROOT,
							ServiceContentConstants.REMOVE_REMOTE_VERSION, p);
				}

			});

			// h1.add(new HTML("&nbsp;&nbsp;"));

			// b = new Button(cons.ret());
			// h1.add(b);
			if (PrivilegChecker.hasPrivilege(context, null))
				v1.add(b1);

			panel.setWidget(v1);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public RemoteVersionRootContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context, String id) {
		this.service = service;
		this.context = context;
		this.cons = cons;
		this.id = id;

		panel = new SimplePanel();
		panel.setWidth("100%");
		initWidget(panel);
	}

	public void onLoad() {
		RequestObject req = new RequestObject();
		req
				.setAction("han.bifang.gwt.server.action.service.GetRemoteVersionDetailAction");
		req.setRequest(id);
		service.call(req, cb);

	}

}
